package com.mask.bank.dao.impl;

import com.mask.bank.dao.AccountDao;
import com.mask.bank.entity.AccountEntity;
import com.mask.bank.entityWapper.AccountRowMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

/**
 * ClassName AccountDaoImpl
 * Create by hx
 * Date 2021/9/16 11:20 上午
 */

@Repository
public class AccountDaoImpl implements AccountDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public int outMoney(String outName,String outCardNumbers, Double money) {
        String sql = "UPDATE account SET money = money - ? WHERE `name` = ? AND card_numbers = ? AND status = 1";
        return jdbcTemplate.update(sql,money,outName,outCardNumbers);
    }

    @Override
    public int inMoney(String inName,String inCardNumbers,Double money) {
        String sql = "UPDATE account SET money = money + ? WHERE `name` = ? AND card_numbers = ? AND status = 1";
        return jdbcTemplate.update(sql,money,inName,inCardNumbers);
    }

    @Override
    public Integer addAccount(AccountEntity accountEntity) {
        String sql = "INSERT INTO account(name,money,card_numbers) VALUES(?,?,?)";
        return jdbcTemplate.update(sql,accountEntity.getName(),
                accountEntity.getMoney(),accountEntity.getCardNumbers());

    }

    @Override
    public Integer deleteAccount(AccountEntity deleteAccount) {
        String sql = "UPDATE account SET status = 0 WHERE name = ? AND card_numbers = ?";
        return jdbcTemplate.update(sql,deleteAccount.getName(),deleteAccount.getCardNumbers());
    }

    @Override
    public AccountEntity queryAccountMoney(AccountEntity queryAccountMoney) {
        String sql = "SELECT * FROM account WHERE name = ? AND card_numbers = ? AND status = 1";
        return jdbcTemplate.queryForObject(sql,new AccountRowMapper(),
                queryAccountMoney.getName(),queryAccountMoney.getCardNumbers());
    }
}
